<template>
  <div>
    <div class="content-order">
      <div class="title">
        <div class="left">
          <span class="logo el-icon-s-order"></span>
          <h1>确认订单</h1>
        </div>
      </div>
    </div>
    <hr />
    <div class="content-order">
      <div class="content">
        <div class="address-title">收货地址</div>
        <div class="updata-imgs">
          <div
            class="updata-img"
            :class="{ active: form.id === item.id }"
            v-for="item in arrAddress"
            :key="item.id"
            @click="updateForm(item)"
          >
            <h2>{{ item.name }}</h2>
            <p>{{ item.phone }}</p>
            <p>{{ item.address }}</p>
          </div>
          <div
            class="updata-img"
            @click="showdialog"
            v-if="arrAddress.length <= 3"
          >
            <div>
              <span class="el-icon-circle-plus"></span>
              <div>添加新地址</div>
            </div>
          </div>
        </div>

        <div class="deprice">商品及优惠券</div>
        <div class="shopdetail">
          <div class="every">
            <div class="shop">
              <img src="../img/noneCar.png" alt="" />
              <span>Redmi Note 11T Pro+ 8GB+128GB 原子银</span>
            </div>
            <div class="onepay">1999元x1</div>
            <div class="ownpay">1999元</div>
          </div>
          <div class="every">
            <div class="shop">
              <img src="../img/noneCar.png" alt="" />
              <span>Redmi Note 11T Pro+ 8GB+128GB 原子银</span>
            </div>
            <div class="onepay">1999元x1</div>
            <div class="ownpay">1999元</div>
          </div>
        </div>
        <div class="email">
          <div>配送方式</div>
          <span>包邮</span>
        </div>
        <div class="card">
          <span>发票</span>
          <div>
            <span>电子普通发票</span><span>个人</span><span>商品明细</span
            ><span><a href="#">修改 ></a></span>
          </div>
        </div>
        <div class="tb">
          <div class="left">
            <div>
              <span class="el-icon-circle-plus fotter"></span>
              <span>使用优惠券</span>
              <span>无可用</span>
            </div>
            <div>
              <span class="el-icon-circle-plus fotter"></span>
              <span>使用小米礼品卡</span>
            </div>
          </div>
          <div class="right">
            <div class="Rkey">商品件数:</div>
            <div class="Rvalue">2件</div>
            <div class="Rkey">商品总价:</div>
            <div class="Rvalue">1999元</div>
            <div class="Rkey">活动优惠:</div>
            <div class="Rvalue">-0元</div>
            <div class="Rkey">优惠券折扣:</div>
            <div class="Rvalue">-0元</div>
            <div class="Rkey">运费:</div>
            <div class="Rvalue">0元</div>
            <div class="Rkey lastRkey">应付总额:</div>
            <div class="Rvalue lastRvalue">元<span>1919</span></div>
          </div>
        </div>
      </div>
      <div class="topay">
        <div>
          <button @click="topay">去结算</button>
          <button @click="tocart">返回购物车</button>
        </div>
      </div>
    </div>
    <!-- 新增地址页面 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleform">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="form.phone" placeholder="请填写手机号"></el-input>
        </el-form-item>
        <el-form-item
          label="收件地址"
          :label-width="formLabelWidth"
          prop="address"
        >
          <el-input
            v-model="form.address"
            placeholder="请填写收件地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAddress">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CorfirmOrder',
  data() {
    const rulePhone = (rule, value, callback) => {
      const reg =
        /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/
      if (!value) {
        callback(new Error('请输入手机号'))
      } else if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('手机格式不规范'))
      }
    }
    return {
      dialogFormVisible: false,
      arrAddress: [],
      form: {
        name: '',
        phone: '',
        address: '',
        id: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, validator: rulePhone, trigger: 'blur' }],
        address: [
          { required: true, message: '请输入收件地址', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    //显示添加地址页面
    showdialog() {
      this.form.name = ''
      this.form.phone = ''
      this.form.address = ''
      this.form.id = ''
      this.$nextTick(() => {
        this.$refs['ruleform'].clearValidate()
      })
      this.dialogFormVisible = true
    },
    //增加地址
    addAddress() {
      this.$refs['ruleform'].validate((valid) => {
        if (valid) {
          const { name, phone, address } = this.form
          const id = parseInt(Math.random() * 100000)
          this.form.id = id
          this.arrAddress.push({
            name,
            phone,
            address,
            id
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    //选择某个地址
    updateForm(item) {
      this.form = { ...item }
    },
    //跳转至结算页面
    topay() {
      for (let item in this.form) {
        this.$store.state.order[item] = this.form[item]
      }
      this.$router.push({
        name: 'Order',
        params: '/order'
      })
    },
    //跳转至购物车页面
    tocart() {
      this.$router.push({
        name: 'ChoppingCart',
        params: '/shoppingcart'
      })
    }
  }
}
</script>
<style scoped>
.shopdetail .every {
  width: 100%;
  display: flex;
  height: 60px;
  line-height: 60px;
  font-size: 14px;
}
.shopdetail .every .shop {
  flex-grow: 12;
}
.shopdetail .every .shop img {
  width: 30px;
  height: 30px;
  vertical-align: middle;
  margin-right: 15px;
}
.shopdetail .every .onepay {
  flex-grow: 6;
}
.shopdetail .every .ownpay {
  flex-grow: 1;
  color: #ff6700;
}
.content-order {
  width: 1200px;
  margin: 0 auto;
}
.title {
  display: flex;
  height: 74px;
  justify-content: space-between;
  line-height: 74px;
}
.title .left {
  display: flex;
}
.title .logo {
  height: 74px;
  line-height: 74px;
  font-size: 48px;
  color: #ff6700;
}
.title h1 {
  font-size: 28px;
  margin: 0;
  margin-left: 30px;
  font-weight: 400;
}
.title .user {
  display: flex;
  font-size: 12px;
  color: #757575;
  align-items: center;
}
.title .user span {
  border-right: 1px solid #757575;
  height: 12px;
  line-height: 12px;
  padding-right: 10px;
}
.title .user div {
  padding-left: 15px;
}
hr {
  border: none;
  height: 2px;
  background-color: #ff6700;
}
.content {
  margin: 30px 5px;
  background-color: #f1f1f1;
  padding: 48px 0 0 20px;
  margin-bottom: 5px;
}
.content .address-title {
  margin-bottom: 20px;
}
.updata-imgs {
  display: flex;
}
.updata-imgs > div {
  margin-right: 15px;
}
.content .updata-img {
  width: 268px;
  height: 178px;
  border: 1px solid #e0e0e0;
  margin-bottom: 20px;
  cursor: pointer;
  padding: 15px 24px 0;
  color: #333;
}
.content .active {
  border-color: #ff6700;
}
.content .updata-img h2 {
  margin-bottom: 10px;
  font-weight: 400;
  font-size: 22px;
}
.content .updata-img p {
  color: #757575;
  padding: 5px 0;
  font-size: 14px;
}
.content .updata-img:hover span {
  color: #b0b0b0;
}
.content .updata-img > div {
  text-align: center;
  margin-top: 30px;
  color: #b0b0b0;
}
.content .updata-img > div span {
  font-size: 30px;
  color: #e0e0e7;
}
.content .updata-img > div div {
  font-size: 14px;
  margin-top: 10px;
}
.deprice {
  font-size: 18px;
}
.shopdetail {
  margin-top: 20px;
  border-bottom: 1px solid #e0e0e0;
}
.email {
  padding: 30px 0;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
}
.email div {
  font-size: 18px;
}
.email span {
  margin-left: 80px;
  font-size: 14px;
  color: #ff6700;
}
.card {
  display: flex;
  padding: 30px 0;
}
.card > span {
  font-size: 18px;
}
.card div {
  display: flex;
  margin-left: 115px;
}
.card div span {
  margin-right: 15px;
  font-size: 14px;
  color: #ff6700;
}
.card div span a {
  color: #ff6700;
}
.tb {
  padding: 10px 30px;
  padding-top: 30px;
  height: 200px;
  display: flex;
  justify-content: space-between;
  color: #757575;
  font-size: 14px;
}
.tb .left div {
  margin-bottom: 25px;
  height: 25px;
  line-height: 25px;
}
.tb .left div:first-child span:last-child {
  color: #999999;
  margin-left: 15px;
}
.right {
  display: flex;
  width: 200px;
  flex-wrap: wrap;
}
.right .Rkey {
  width: 100px;
  display: flex;
  flex-direction: row-reverse;
}
.right .Rvalue {
  width: 100px;
  display: flex;
  flex-direction: row-reverse;
  color: #ff6700;
}
.right .lastRvalue,
.right .lastRkey {
  height: 40px;
  align-items: center;
}
.right .Rvalue span {
  font-size: 30px;
  line-height: 40px;
}
.topay {
  height: 80px;
  background-color: #f1f1f1;
  padding-top: 20px;
  margin: 0 5px;
}
.topay div {
  height: 40px;
  display: flex;
  flex-direction: row-reverse;
}
.topay div button {
  width: 160px;
  height: 40px;
  margin-right: 30px;
  border: none;
  cursor: pointer;
}
.topay div button:last-child {
  color: #b0b0b0;
  border: 1px solid #b0b0b0;
}
.topay div button:first-child {
  color: #fff;
  background-color: #ff6700;
}
.fotter {
  font-size: 22px;
  color: #ff6700;
  margin-right: 10px;
  vertical-align: middle;
}
</style>
